<?php
/**
 * Created by PhpStorm.
 * User: ruma
 * Date: 2019/1/24
 * Time: 17:50
 */

namespace app\admin\controller;


use app\common\controller\AdminBaseController;
use app\admin\model\Category as CategoryModel;
use app\admin\model\Article as ArticleModel;
use app\admin\model\ArcTag as ArcTagModel;
use app\Excepiton\ParameterException;
use think\Db;

class Article extends AdminBaseController
{
    protected $db;

    protected function _initialize() {
        parent::_initialize();
        $this->db = new ArticleModel();
    }

    public function index(){
        $art = $this->db->getAllArticle();
        $this->assign('art',$art);
        return $this->fetch();
    }

    /**
    *添加文章
     */
    public function addArt(){
        //1.遍历出栏目
        $cate = (new CategoryModel())->getAllCate();
        $this->assign('cate',$cate);
        //2.遍历出标签
        $tag = Db::table('tag')->select();
        //3.判断是否提交提交数据
        if (IS_POST){
            try{
                $this->db->addArticleData();
            }
            catch (ParameterException $e){
                $str = arrayToString($e->msg);
                $this->error('添加失败,'.$str);
            }
            $this->success('添加成功','index');
        }

        $this->assign('tag',$tag);
        return $this->fetch();
    }


    //文章编辑功能
    public function edit(){
        //4.更新数据
        if (IS_POST){
            try{
                $this->db->editArticleData();
            }
            catch (ParameterException $e){
                $str = arrayToString($e->msg);
                $this->error('修改失败,'.$str);
            }
            $this->success('修改成功','index');
        }
        $arc_id = input('param.arc_id');
        //1.依然要遍历栏目
        $cate = (new CategoryModel())->getAllCate();
        $this->assign('cate',$cate);
        //2.遍历所选标签和标签列表
        $tag = Db::table('tag')->select();
        $selectd_tag = (new ArcTagModel)->getArcTagById($arc_id);
        $this->assign('tag',$tag);
        $this->assign('arc_tag',json_encode($selectd_tag));
        //3.获取相关文章数据
        $art_data = $this->db->getArticleDataById($arc_id);
        $this->assign('arc',$art_data);
        return $this->fetch();
    }

    //回收站管理
    public function recycle(){
        $arc = $this->db->getRecycleArticle();
        $total = ($arc->toArray())['total'];
//        halt($total);
        $this->assign('arc',$arc);
        $this->assign('total',$total);
        return $this->fetch();

    }

    //文章删除至回收站
    public function delToRecycle($arc_id){
        Db::table('article')->where('arc_id','=',$arc_id)->update(['is_recycle'=>'1']);
        return $this->success('删除成功','index');
    }

    //文章移除回收站(恢复数据)
    public function outToRecycle($arc_id){
        Db::table('article')->where('arc_id','=',$arc_id)->update(['is_recycle'=>'2']);
        return $this->success('恢复数据成功','index');
    }

    //实际删除文章数据
    public function del(){
        if (IS_POST){
            $arc_id = input('post.arc_id');
            Db::table('article')->where('arc_id','=',$arc_id)->delete();
            return $this->success('删除文章成功','index');
        }
        return $this->error('删除文章失败','index');
    }




}